package me.zhengjie.modules.inspection.rest;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import me.zhengjie.aop.log.Log;
import me.zhengjie.modules.inspection.domain.InspectionItem;
import me.zhengjie.modules.inspection.domain.InspectionTemplate;
import me.zhengjie.modules.inspection.domain.InspectionTemplateItemRel;
import me.zhengjie.modules.inspection.service.InspectionTemplateItemRelService;
import me.zhengjie.modules.inspection.service.InspectionTemplateService;
import me.zhengjie.modules.inspection.service.dto.InspectionItemDTO;
import me.zhengjie.modules.inspection.service.dto.InspectionTemplateDTO;
import me.zhengjie.modules.inspection.service.dto.InspectionTemplateQueryCriteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
* @author xuyukun
* @date 2019-07-03
*/
@RestController
@RequestMapping("api")
public class InspectionTemplateController {

    @Autowired
    private InspectionTemplateService inspectionTemplateService;
    @Autowired
    private InspectionTemplateItemRelService inspectionTemplateItemRelService;
    @Log("查询InspectionTemplate")
    @GetMapping(value = "/inspectionTemplate")
    @PreAuthorize("hasAnyRole('ADMIN','INSPECTIONTEMPLATE_ALL','INSPECTIONTEMPLATE_SELECT')")
    public ResponseEntity getInspectionTemplates(InspectionTemplateQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity(inspectionTemplateService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @Log("新增InspectionTemplate")
    @PostMapping(value = "/inspectionTemplate")
    @PreAuthorize("hasAnyRole('ADMIN','INSPECTIONTEMPLATE_ALL','INSPECTIONTEMPLATE_CREATE')")
    public ResponseEntity create(@Validated @RequestBody InspectionTemplateDTO inspectionTemplateDTO){
        InspectionTemplate inspectionTemplate=new InspectionTemplate();
        inspectionTemplate.setObjName(inspectionTemplateDTO.getObjName());
        inspectionTemplate.setObjId(inspectionTemplateDTO.getObjId());
        inspectionTemplate.setTemplateDesc(inspectionTemplateDTO.getTemplateDesc());
        inspectionTemplate.setTemplateName(inspectionTemplateDTO.getTemplateName());
        inspectionTemplate.setTypeId(inspectionTemplateDTO.getTypeId());
        inspectionTemplate.setTypeName(inspectionTemplateDTO.getTypeName());
        InspectionTemplateDTO inspectionTemplateDTO1=inspectionTemplateService.create(inspectionTemplate);
        for(InspectionItem inspectionItem:inspectionTemplateDTO.getCheckBoxDatas()){
            InspectionTemplateItemRel inspectionTemplateItemRel=new InspectionTemplateItemRel();
            inspectionTemplateItemRel.setItemId(inspectionItem.getId());
            inspectionTemplateItemRel.setTemplateId(inspectionTemplateDTO1.getId());
            inspectionTemplateItemRelService.create(inspectionTemplateItemRel);
        }
        return new ResponseEntity(inspectionTemplateDTO1,HttpStatus.CREATED);

    }

    @Log("修改InspectionTemplate")
    @PutMapping(value = "/inspectionTemplate")
    @PreAuthorize("hasAnyRole('ADMIN','INSPECTIONTEMPLATE_ALL','INSPECTIONTEMPLATE_EDIT')")
    public ResponseEntity update(@Validated @RequestBody InspectionTemplate resources){
        inspectionTemplateService.update(resources);
        return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    @Log("删除InspectionTemplate")
    @DeleteMapping(value = "/inspectionTemplate/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','INSPECTIONTEMPLATE_ALL','INSPECTIONTEMPLATE_DELETE')")
    public ResponseEntity delete(@PathVariable Long id){
        inspectionTemplateService.delete(id);
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log("根据objectid查")
    @GetMapping(value = "/inspectionTemplate/byObjectId/{id}")
    @PreAuthorize("hasAnyRole('ADMIN','DICT_ALL','DICT_SELECT')")
    public ResponseEntity byObjectId(@PathVariable  int  id){
        return new ResponseEntity(inspectionTemplateService.findByObjectId(Long.valueOf(id)),HttpStatus.OK);
    }
}